package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.view.View;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.rules.StocksBean;
import com.wwwarehouse.resource_center.eventbus_event.Event;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/11/3.
 *         <p/>
 *         选择应用对象
 */

public class SelectObjectFragment extends MyBaseViewPagerFragment {

    private int num  = 9;
    private String businessId ;
    private  int total ;
    private ArrayList<StocksBean.ListBean> mSelectList = new ArrayList<>();
    private ArrayList<StocksBean.ListBean> mList;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof SelectObjectFragment)
            mActivity.setTitle(getString(R.string.seslect_task_obj));
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        if (event.getCode()==RulesEvent.RULE_SELECT_STOCKS){
            StocksBean.ListBean  bean = (StocksBean.ListBean) event.getData();
            if (null!=bean){
                LogUtils.showInfoLog("notify:"+bean.toString());
                //可能同一个，一个是true，一个是false
                if (!mSelectList.contains(bean)){
                    if (mSelectList.size()>0){
                        for (int i = 0; i < mSelectList.size(); i++) {
                            if (mSelectList.get(i).getStockUkid().equals(bean.getStockUkid())&&!bean.isSelect()){
                                mSelectList.remove(i);
                                break;
                            }
                            if (!mSelectList.get(i).getStockUkid().equals(bean.getStockUkid())&&bean.isSelect()){
                                mSelectList.add(bean);
                                break;
                            }
                        }
                    }else {
                        mSelectList.add(bean);
                    }
                }else {
                    if (!bean.isSelect()){
                        mSelectList.remove(bean);
                    }
                }
            }
            int selectTagCount = mSelectList.size();
            mConfirmBtn.setVisibility(selectTagCount>0?View.VISIBLE:View.GONE);
            if (selectTagCount > 0) {
                mActivity.setTitle("选择应用对象(" + selectTagCount + ")");
            } else {
                mActivity.setTitle("选择应用对象");
            }
        }
    }

    @Override
    protected void loadDatas() {
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确认
                EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_TASK,mSelectList,"RULE_STOCKS"));
                popFragment();
            }
        });
        Bundle bundle = getArguments();
        if (bundle!=null){
            total = bundle.getInt("total");
            mList = bundle.getParcelableArrayList("mStocksListBean");
            SetFragmentAndPage(total,num,new SelectObjectListFragment().getClass().getName(),bundle);
        }
        if (mList!=null&&mList.size()>0){
            mSelectList.addAll(mList);
            mConfirmBtn.setVisibility(View.VISIBLE);
            mActivity.setTitle("选择应用对象("+mSelectList.size()+")");
        }else {
            mActivity.setTitle("选择应用对象");
        }
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onDownPull() {

    }
}
